package com.aaa.system.service.imp;

import com.aaa.entity.CheckItem;
import com.aaa.entity.User;
import com.aaa.system.mapper.InsFeeMapper;
import com.aaa.system.mapper.UserMapper;
import com.aaa.system.service.InsFeeService;
import com.aaa.utils.TokenUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class InsFeeServiceImp implements InsFeeService {

    @Resource
    private InsFeeMapper insFeeMapper;

    @Resource
    private UserMapper userMapper;
    @Override
    public Result<IPage<CheckItem>> findAllInsFeeData(Long current, Long size, CheckItem checkItem) {
        IPage<CheckItem> page = new Page<>(current,size);
        QueryWrapper<CheckItem> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(checkItem.getCheckItemName())){
            wrapper.like("check_item_name",checkItem.getCheckItemName());
        }
        if (StringUtils.isNotBlank(checkItem.getKeywords())){
            wrapper.like("keywords",checkItem.getKeywords());
        }
        if (Objects.nonNull(checkItem.getTypeId())){
            wrapper.eq("type_id",checkItem.getTypeId());
        }
        if (Objects.nonNull(checkItem.getStatus())){
            wrapper.eq("status",checkItem.getStatus());
        }
        IPage<CheckItem> page1 = insFeeMapper.selectPage(page, wrapper);
        return new Result<>(200,"查询成功",page1);
    }

    @Override
    public Result<Boolean> saveOrUpdateInsFee(CheckItem checkItem) {
        //获取创建人的信息
        String token = WebUtil.getRequest().getHeader("token");
        String phone = TokenUtils.getInfo(token).get("username").toString();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        String userName = userMapper.selectOne(wrapper).getUserName();

        if (Objects.nonNull(checkItem.getCheckItemId())){
            //修改
            checkItem.setUpdateBy(userName);
            checkItem.setUpdateTime(LocalDateTime.now());
            int i = insFeeMapper.updateById(checkItem);
            return new Result<>(200,"修改检查项目成功",i>0?true:false);
        }
        //添加
        checkItem.setCreateBy(userName);
        checkItem.setCreateTime(LocalDateTime.now());
        int i = insFeeMapper.insert(checkItem);
        return new Result<>(200,"添加检查项目成功",i>0?true:false);
    }

    @Override
    public Result<Boolean> delInsFeeData(Long[] checkItemIds) {
        int i = insFeeMapper.bachInsFeeData(checkItemIds);
        return new Result<>(200,"删除检查项目成功",i>0?true:false);
    }

    /**
     * 调用使用
     * @return
     */
    @Override
    public List<CheckItem> findItemNameData() {
        QueryWrapper<CheckItem> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        List<CheckItem> checkItems = insFeeMapper.selectList(wrapper);
        return checkItems;
    }
}
